/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import models.User;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import service.HibernateUtil;
import service.UserDao;

/**
 *
 * @author kjeld
 */
public class LogInController extends HttpServlet {

    private HibernateUtil HU = new HibernateUtil();
    private Session session = HU.openSession();
    private UserDao dao = new UserDao(session);

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        forward(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        String userName = new String();
        String passWord = new String();
        if (request.getParameter("userName") != null) {
            userName = request.getParameter("userName");
        }
        if (request.getParameter("passWord") != null) {
            passWord = request.getParameter("passWord");
        }
        Criteria usersCrit = session.createCriteria(User.class).add(Restrictions.in("userName", new String[]{userName})).add(Restrictions.in("passWord", new String[]{passWord}));
        List users = usersCrit.list();

        if (users.size() > 0) {
            User user = (User) users.get(0);
            HttpSession httpSession = request.getSession();
            httpSession.setAttribute("user", user);
            httpSession.setAttribute("loggedin", "true");
            Cookie userID = new Cookie("userID", user.getUserID() + "");
            Cookie firstname = new Cookie("firstname", user.getFirstName());
            Cookie lastname = new Cookie("lastname", user.getLastName());
            Cookie username = new Cookie("username", user.getUserName());
            Cookie status = new Cookie("status", user.getStatus());
            //Cookie gender = new Cookie("gender", user.getGender());
            //Cookie birthdate = new Cookie("birthdate", user.getBirthDate().toString());
            //Cookie address = new Cookie("address", user.getAddress());
            //Cookie zipcode = new Cookie("zipcode", user.getZipCode());
            //Cookie city = new Cookie("city", user.getCity());
            //Cookie email = new Cookie("email", user.getEmail());
            //Cookie phone = new Cookie("phone", user.getPhoneNumber());

            /*firstname.setMaxAge(60 * 60 * 24);
             lastname.setMaxAge(60 * 60 * 24);
             username.setMaxAge(60 * 60 * 24);
             status.setMaxAge(60 * 60 * 24);
             address.setMaxAge(60 * 60 * 24);
             zipcode.setMaxAge(60 * 60 * 24);
             city.setMaxAge(60 * 60 * 24);
             birthdate.setMaxAge(60 * 60 * 24);
             gender.setMaxAge(60 * 60 * 24);
             email.setMaxAge(60 * 60 * 24);
             phone.setMaxAge(60 * 60 * 24);*/
            response.addCookie(userID);
            response.addCookie(firstname);
            response.addCookie(lastname);
            response.addCookie(username);
            response.addCookie(status);
            /*response.addCookie(zipcode);
             response.addCookie(address);
             response.addCookie(birthdate);
             response.addCookie(gender);
             response.addCookie(city);
             response.addCookie(email);
             response.addCookie(phone);*/
            if (user.getStatus().equals("Student")) {
                httpSession.setAttribute("omgeving", "student");
                response.sendRedirect("/student");
            } else if (user.getStatus().equals("Admin")) {
                httpSession.setAttribute("omgeving", "admin");
                response.sendRedirect("/admin");
            } else if (user.getStatus().equals("Tutor")) {
                httpSession.setAttribute("omgeving", "tutor");
                response.sendRedirect("/tutor");
            } else {
                response.sendRedirect("/error.jsp");
            }
        } else {
            response.sendRedirect("/error.jsp");
        }
    }

    private void forward(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String address = "/";
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);
    }
}
